<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include::header"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>   </h5>
					</div>
					<div class="ibox-content">
						<form class="form-horizontal m-t" id="signupForm">
							<input type="hidden" id="roleId" name="roleId" th:value="${role.roleId}">
							<input type="hidden" name="menuIds" id="menuIds">
							<div class="form-group">
								<label class="col-sm-3 control-label">角色名：</label>
								<div class="col-sm-8">
									<input  class="form-control" name="roleName" th:value="${role.roleName}"
										type="text">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">备注：</label>
								<div class="col-sm-8">
									<input  class="form-control" name="remark"
										type="text" th:value="${role.remark}">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">菜单权限：</label>
								<div class="col-sm-8">
									<div id="menuTree">
									</div>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-8 col-sm-offset-3">
									<button type="submit" class="btn btn-primary">提交</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div th:include="include::footer"></div>
</body>
<script th:inline="javascript">
	$.validator.setDefaults({
		submitHandler:function(){
		    save();
		}
	})
	function save(){
	    getSelectIds();
	    $.ajax({
			url:'/sys/role/update',
			data:$('#signupForm').serialize(),
			dataType:'json',
			type:'post',
			success:function(data){
				if(data.code==200){
					// chenggong
					// 关闭弹窗  刷新列表
					parent.reload();
					var frameIndex = 	parent.layer.getFrameIndex(window.name);
					parent.layer.close(frameIndex);
				}else {
				    //失败
					parent.layer.alert('修改失败',{icon:2});
				}
			}
		})
	}
	function getSelectIds(){
	    // 获取所选的 menuIds
        var rows = $('#menuTree').jstree(true).get_selected();
		// 把menuIds放入  一个隐藏的input
		$('#menuIds').val(rows);
	}

	$(function(){
	    // 生成树
		createJsTree();
		// 绑定验证  roleId
		$('#signupForm').validate({
			rules:{
                roleName:'required'
			},
			messages:{
				roleName:{
				    required:'角色名称不能为空'
				}
			}
		});
	})
	function createJsTree(){
	    var url = '/sys/menu/tree/[[${role.roleId}]]';
	    $.ajax({
			url:url,
			dataType:'json',
			type:'post',
			success:function(treeData){
				$('#menuTree').jstree({
					core:{
					    data:treeData
					},
					plugins:['checkbox','wholerow']
				});
			}
		});
	}
</script>
</html>
